import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios';
import cartModule from './cartModule'

Vue.use(Vuex)

export default new Vuex.Store({
  // 保持组件状态
  state: { 
    Alllist:[],  
    smlist:[],
    cenlist:[],
    biglist:[]
  },
  // 修改组件状态 同步操作
  mutations: {
    // 列表页数据
    ALL_LIST(state,items){
      state.Alllist = [...items];
    },
    ALL_sm_LIST(state,items){
      state.smlist = [...items];
    },
    ALL_cen_LIST(state,items){
      state.cenlist = [...items];
    },
    ALL_big_LIST(state,items){
      state.biglist = [...items];
    },
    // 排序
    Sort_PRICE_GOODS(state, num) { 
      if (num % 2 == 0) {
        state.Alllist.sort((a, b) => {
          return a.salePrice - b.salePrice
        })
        state.smlist.sort((a, b) => {
          return a.salePrice - b.salePrice
        })
        state.cenlist.sort((a, b) => {
          return a.salePrice - b.salePrice
        })
        state.biglist.sort((a, b) => {
          return a.salePrice - b.salePrice
        })
      } else if (num % 2 != 0) {
        state.Alllist.sort((a, b) => {
          return b.salePrice - a.salePrice
        })
        state.smlist.sort((a, b) => {
          return b.salePrice - a.salePrice
        })
        state.cenlist.sort((a, b) => {
          return b.salePrice - a.salePrice
        })
        state.biglist.sort((a, b) => {
          return b.salePrice - a.salePrice
        })
      }
    },
  },
  // 触发 mutations
  actions: {
    // 列表页数据  总数据
    ALL_LIST(context){
      axios.get("data1.json").then((res) => {
          console.log(res.data.result.list);
          context.commit("ALL_LIST",res.data.result.list);
      }).catch((err) => {
          console.log(err);
      })
    },
    // 0-1000
    ALL_sm_LIST(context){
      axios.get("data.json").then((res) => {
        console.log(res.data.result.list)
        context.commit("ALL_sm_LIST",res.data.result.list);
      })
    },
    // 1000-2500
    ALL_cen_LIST(context){
      axios.get("data2.json").then((res) => {
        console.log(res.data.result.list)
        context.commit("ALL_cen_LIST",res.data.result.list);
      })
    },
    // 2500
    ALL_big_LIST(context){
      axios.get("data3.json").then((res) => {
        console.log(res.data.result.list)
        context.commit("ALL_big_LIST",res.data.result.list);
      })
    }
  }, 
  modules: {
    cartModule
  }
})
 